package com.hotcode;

import java.util.*;

/**
 * @Author: huangzhigao
 * @Date: 2023/2/25 15:55
 */
public class leecode49 {
    public static List<List<String>> groupAnagrams(String[] strs) {
        if (strs.length == 0) {
            return null;
        }
        Map<String, List<String>> res = new HashMap<>();
        for (int i = 0; i < strs.length; i++) {
            String oldVar = strs[i];
            char[] oldChars = oldVar.toCharArray();
            Arrays.sort(oldChars);
            if (!res.containsKey(String.valueOf(oldChars))) {
                res.put(String.valueOf(oldChars), new ArrayList<>());
            }
            res.get(String.valueOf(oldChars)).add(oldVar);
        }
        List<List<String>> answer = new ArrayList<>();
        for (Map.Entry<String, List<String>> entry : res.entrySet()) {
            answer.add(entry.getValue());
        }
        return answer;
    }


    public static void main(String[] args) {
        String[] ee = {"eat", "tea", "tan", "ate", "nat", "bat"};
        groupAnagrams(ee);
    }
}
